package com.lc.controller;

import com.lc.constant.Constants;
import com.lc.pojo.UmsPcdDic;
import com.lc.pojo.dto.UmsReceiverAddressDto;
import com.lc.pojo.param.SaveOrUpdateParam;
import com.lc.result.Result;
import com.lc.result.ResultUtils;
import com.lc.service.UmsPcdDicService;
import com.lc.service.UmsReceiverAddressService;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Description: 收货地址
 * @Author lc
 */

@RestController
@RequestMapping("/ums/receiverAddress")
public class UmsReceiverAddressController {

    @Resource
    private UmsPcdDicService umsPcdDicService;

    @Resource
    private UmsReceiverAddressService uraService;

    //添加、修改收货地址接口
    @PostMapping("/saveOrUpdate")
    public  Result saveOrUpdate(SaveOrUpdateParam saveOrUpdateParam, HttpServletRequest request){
        String token = request.getHeader(Constants.Redis.PREFIX_TOKEN);
        if(!StringUtils.hasLength(token)){
            return ResultUtils.returnFail("添加、修改收货地址前，请登录");
        }

        Boolean saveOrUpdateURA = uraService.saveOrUpdateURA(saveOrUpdateParam);
        if(saveOrUpdateURA){
            return ResultUtils.returnSuccess();
        }
        return ResultUtils.returnFail();
    }

    //删除收货地址接口
    @PostMapping("/remove")
    public Result removeURA(Integer addressId,HttpServletRequest request){
        String token = request.getHeader(Constants.Redis.PREFIX_TOKEN);
        if(!StringUtils.hasLength(token)){
            return ResultUtils.returnFail("删除收货地址前，请登录");
        }

        Boolean removeURA = uraService.removeURA(addressId);
        if(removeURA){
            return ResultUtils.returnSuccess();
        }
        return ResultUtils.returnFail();
    }

    //查看收货地址接口
    @GetMapping("/list")
    public Result list(Integer userId, HttpServletRequest request){
        String token = request.getHeader(Constants.Redis.PREFIX_TOKEN);
        if(!StringUtils.hasLength(token)){
            return ResultUtils.returnFail("查看收货地址前，请登录");
        }

        List<UmsReceiverAddressDto> list = uraService.getList(userId);
        if(!list.isEmpty()){
            return ResultUtils.returnDataSuccess(list);
        }
        return ResultUtils.returnFail();
    }

    //设置默认收货地址接口
    @PostMapping("/default")
    public Result  updateDefault(Integer addressId,HttpServletRequest request){
        String token = request.getHeader(Constants.Redis.PREFIX_TOKEN);
        if(!StringUtils.hasLength(token)){
            return ResultUtils.returnFail("设置默认收货地址前，请登录");
        }

        Boolean updatedDefault = uraService.updateDefault(addressId);
        if(updatedDefault){
            return ResultUtils.returnSuccess();
        }
        return ResultUtils.returnFail();
    }

    //省市区列表接口
    @GetMapping("/pcdList")
    public Result pcdList(){
        List<UmsPcdDic> list = umsPcdDicService.getPcdList();
        if(!list.isEmpty()){
            return ResultUtils.returnDataSuccess(list);
        }
        return ResultUtils.returnFail();
    }

}
